//
//  SinaHomeComposeView.swift
//  MySinaWeiBoSwiftProject
//
//  Created by apple on 2016/11/21.
//  Copyright © 2016年 WangChen. All rights reserved.
//

import UIKit
import pop


class SinaHomeComposeView: UIView {

    
    //按钮数组
    private lazy var writeBtnArray = [SinaWriteButton]()
    
    
    
    override init(frame: CGRect) {
        super.init(frame: CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight))
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK:
    // MARK: - 展示撰写界面
    func show(fromVc: UIViewController?) {
        
        fromVc?.view.addSubview(self)
        for (index,btn) in writeBtnArray.enumerated(){
            self.setWriteBtnAnim(btn: btn, index: index, isShow: true)
        }
        
    }
    
    
    
    // MARK:
    // MARK: - 布局界面
    func setupUI() {
        
        self.addSubview(backImageView)
        setupImageView()
        addWriteBtn()
        
    }
    
    // MARK:
    // MARK: - 毛玻璃效果
    private lazy var backImageView: UIImageView = UIImageView(image: UIImage.snapCurrentScreenImage().applyLightEffect())
    
    // MARK:
    // MARK: - 点击界面
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        for (index, btn) in writeBtnArray.enumerated() {
            setWriteBtnAnim(btn: btn, index: index, isShow: false)
        }
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.5) {
            self.removeFromSuperview()
        }
        
    }
    
    // MARK:
    // MARK: - 添加上面的图片
    // compose_slogan
    private func setupImageView() {
        
        let iv = UIImageView(image: #imageLiteral(resourceName: "compose_slogan"))
        self.addSubview(iv)
        iv.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-200)
        }
    }
    
    
    
    
    // MARK:
    // MARK: - 添加撰写按钮
    private func addWriteBtn(){
        
        for i: Int in 0..<6{
            let btn = SinaWriteButton()
            
            //确定行列
            let col = i % 3
            let row = i / 3
            
            let btnMargin: CGFloat = (screenWidth - 3 * 80) / 4
            
            let x = btnMargin + (btnMargin + 80) * CGFloat(col)
            let y = (btnMargin + 110) * CGFloat(row) + screenHeight
            
            
            btn.frame = CGRect(x: x, y: y, width: 80, height: 110)
            
            self.addSubview(btn)
            
            btn.setTitle("发微博", for: .normal)
            btn.setImage(#imageLiteral(resourceName: "tabbar_compose_idea"), for: .normal)
            writeBtnArray.append(btn)
            
            //按钮点击监听
            btn.addTarget(self, action: #selector(writeBtnDidClick(btn:)), for: .touchUpInside)
            
        }
        
    }
    
    
    // MARK:
    // MARK: - 给撰写按钮添加动画
    private func setWriteBtnAnim(btn: SinaWriteButton, index: Int, isShow: Bool) {
        
        let springAnimation = POPSpringAnimation(propertyNamed: kPOPViewCenter)
        
        springAnimation?.springSpeed = 10
        springAnimation?.springBounciness = 10
        
        springAnimation?.beginTime = CACurrentMediaTime() + Double(index) * 0.025
        
        let btnCenterY = btn.center.y + CGFloat(isShow ? -350 : 350)
        
        springAnimation?.toValue = NSValue.init(cgPoint: CGPoint(x: btn.center.x, y: btnCenterY))
        
        btn.pop_add(springAnimation, forKey: nil)
    }
    
    
    
    // MARK:
    // MARK: - 监听按钮点击
    @objc private func writeBtnDidClick(btn: SinaWriteButton){
        
        UIView.animate(withDuration: 0.5, animations: {
            for button in self.writeBtnArray{
                if button == btn{
                    button.transform = CGAffineTransform.init(scaleX: 2, y: 2)
                    button.alpha = 0.1
                }else{
                    button.transform = CGAffineTransform.init(scaleX: 0.2, y: 0.2)
                    button.alpha = 0.1
                }
            }
        }) { (bool) in
            
            //跳转对应的控制器
            let bundleName = Bundle.main.infoDictionary!["CFBundleName"] as! String
            let className = bundleName + ".SinaComposeController"
            
            guard let classType = NSClassFromString(className) as? UIViewController.Type else {
                return
            }
            
            let composeVC = classType.init()
            let composeNavi = UINavigationController(rootViewController: composeVC)
            
            UIApplication.shared.keyWindow?.rootViewController?.present(composeNavi, animated: true, completion: {
                self.removeFromSuperview()
            })
        }
        
    }
    

}
